#include <iostream>
#include <string>
#include <vector>
#include "mprpcapplication.h"
#include "rpcprovider.h"
#include "friend.pb.h"

class FriendService : public fixbug::FriendServiceRpc {
public:
    // 服务器本地方法
    std::vector<std::string> GetFriendsList(uint32_t userid) {
        std::cout << "do GetFriendsList service! userid:" << userid << std::endl;
        std::vector<std::string> vec;
        vec.push_back("gao yang");
        vec.push_back("yu qing");
        vec.push_back("peng fei");

        return vec;
    }

    void GetFriendsList(::google::protobuf::RpcController* controller,
                        const ::fixbug::GetFriendsListRequest* request,
                        ::fixbug::GetFriendsListResponse* response,
                        ::google::protobuf::Closure* done) {
        // 开始业务，通过序列化的请求提取出数据
        uint32_t userid = request->userid(); 

        // 本地方法调用
        std::vector<std::string> friendsList = GetFriendsList(userid);

        // 响应写入
        fixbug::ResultCode* code = response->mutable_result();
        code->set_errcode(0);
        code->set_errmsg("");
        for(auto& name : friendsList) {
            std::string* p = response->add_friends();
            *p = name;
        }

        // 执行回调函数
        done->Run();                    
    }
};

int main(int argc, char* argv[]) {
    // 远程调用 框架初始化 provider -i config.conf
    MprpcApplication::Init(argc, argv);

    // 发布服务到rpc节点上
    RpcProvider provider;
    provider.NotifyService(new FriendService());

    // 启动rpc节点，进程进入阻塞状态，等待远程rpc调用
    provider.Run();

    return 0;
}